package com.zhaiweijia.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.zhaiweijia.entity.Article;
import com.zhaiweijia.entity.Category;
import com.zhaiweijia.entity.Channel;
import com.zhaiweijia.entity.User;
import com.zhaiweijia.service.ArticleService;
import com.zhaiweijia.service.ChannelService;
import com.zhaiweijia.util.RandomUtil;

@Controller
@RequestMapping("my")
public class MyController {
		
	@Autowired
	private ChannelService channelService;
	@Autowired
	private ArticleService articleService;
	
	//跳转个人中心
	@RequestMapping({"/","","index"})
	public String index() {
		
		return "my/index";
	}
	/**
	 * 根据文章id查数据
	 * @param id
	 * @return
	 */
	@RequestMapping("detail")
	@ResponseBody
	public Article detail(Integer id) {
		
		Article article = articleService.findDetailById(id);
		
		
		return article;

	}
	
	/**
	 * 查询当前用户的所有文章列表
	 * @param pageNum
	 * @param pageSize
	 * @param article
	 * @param model
	 * @return
	 */
	@RequestMapping("articles")
	public String articles(@RequestParam(defaultValue = "1")Integer pageNum,
			@RequestParam(defaultValue = "3")Integer pageSize,Model model) {
		//先写死用户
		User user = new User();
		user.setId(1);
		//查询数据		
		
		PageInfo info=articleService.listByUser(pageNum,pageSize,user);
		model.addAttribute("info", info);
		
		return "my/articles";
		
	}
	
	@RequestMapping("publish")
	@ResponseBody
	public Boolean publish(Article article,MultipartFile file) throws IllegalStateException, IOException {
		//处理图片
		try {
			if(file !=null && file.getSize()>0 ) {
				  
				  String filename = file.getOriginalFilename();
				  filename=RandomUtil.uuid()+filename.substring(filename.lastIndexOf("."));
				  File file2 = new File("d:/img/"+filename);
				  
				  file.transferTo(file2);
				  
				  article.setPicture("/img/"+filename);
				  
				  //文章其他字段
				  article.setUserId(1);
				  article.setHits(0);//点击量
				  article.setHot(0);//是否热门
				  article.setStatus(0);////0,刚发布,1审核通过,-1 审核未通过
				  article.setDeleted(0);//0:正常,1:删除
				  article.setCreated(new Date());//文章创建日期
				  article.setContentType(0);//文章类型0：html；1:json
				  article.setComments(0);//评论数量
				  //将数据存入到数据库
				  articleService.add(article);
				  
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
	
	
	@RequestMapping("getCategoryIdByChannelId")
	@ResponseBody
	public List<Category> getCategoryIdByChannelId(Integer channelId) {
		
		List<Category> category=channelService.getCategoryIdByChannelId(channelId);
		
		return category;
	
	}
	
	@RequestMapping("getChannels")
	@ResponseBody
	public List<Channel> getChannels() {
		
		List<Channel> list=channelService.getChannels();
		System.out.println(list);
		return list;
	
	}
	
	
	@GetMapping("publish")
	public String publish() {
	
		return "my/publish";
	
	}
	
}
